#!/usr/bin/perl -w
use strict;
use POSIX qw(strftime);

###############################
# Move finished tvshow file 
# by rift the roXXXXori
#
# Param :
#     --debug : verbose mode without writing changes
#     --cron <file> : cron mode, write the log to the file
#
###############################

# prefix for the season directory (with space !!)
my $PREFIX_SEASON = "saison ";
# downoad directory 
my $DIR_DOWNLOAD = "/data/download";
# final directory
my $DIR_SERIES = "/data/series";
# cron log
my $LOG_CRON = "/root/log/tvshowcleaner.log";
# date
my $date = strftime "%d-%m-%y", localtime;

my @args;
my @tvshows;
my @files;

my $debug = 0;
my $cron = 0;

my $arg;
my $show;
my $file;
my $i;
my $dir;
my $season;

@args = @ARGV;

# tv show array
# the first string is the tvshow
# the second is the dir on the final dir, leave empty if --debug find it
@tvshows  = (	["tvshow1",			""],
		["tvshow2",			"dirtvshow2"]);

#
# Debug fonction, write log to the stdin
#
sub debug { 
   my $param = $_[0];
   if ($debug == 1) {
      print $param;
   } 
}

#
# Cron fonction, write log to the cronlog
#
sub cron_log {
   my $param = $_[0];
   if ($cron == 1) {
      print FILE "$param\n";
   }
}

#
# Searching for args
#
foreach $arg(@args) {
   if ($arg eq "--debug") {
      $debug = 1;
   } else {
      if ($arg eq "--cron") {
	 $cron = 1;
      } else {
	 print "|W| Unknown argument : $arg\n";
      }
   }
}

#
# Open cron log file if needed
#
if ($cron == 1) { 
   open(FILE, '>>',"$LOG_CRON") || die "Cron log error:$!\n";
   cron_log("####################################");
   cron_log("Script started ".$date);
}

# Reading the array of tvshow
foreach $show(@tvshows) { 
   $dir = "";
   $i = $$show[0];
   $i =~ s/\s+/./g;	
   @files = ();
   $season = "";

   # Get final directory 
   if ($$show[1] eq "") {
      $dir = `ls $DIR_SERIES |grep -i "$$show[0]"`;
      chomp($dir);
   } else { $dir = $$show[1]; }

   # Print info in debug mode about the show
   debug("|I| Show: ".$$show[0]." Dir: ".$dir."\n");

   # Search for the file 
   @files = `ls $DIR_DOWNLOAD |grep -i $i|grep -v .part`;
   foreach $file (@files) {
      chomp($file);

      # Find the season of the file 
      if ($file =~ /(.*)\bS(\d+)e(\d+)/i) { 
	 $season = $2;
	 $season = int($season)
      }

      # Print info in debug mode about the file 
      debug("  |M| File: $file ($PREFIX_SEASON $season) \n");

      # Create final dir
      debug("  |T| Creating directory $DIR_SERIES/$dir/$PREFIX_SEASON$season\n");
      if (!$debug) {
	 `mkdir -p "$DIR_SERIES/$dir/$PREFIX_SEASON$season"`;
      }
	
      # Move the file 
      debug("    |T| Moving $DIR_DOWNLOAD/$file to $DIR_SERIES/$dir/$PREFIX_SEASON$season/$file\n");
      cron_log("       Moving $DIR_DOWNLOAD/$file to $DIR_SERIES/$dir/$PREFIX_SEASON$season/$file\n");
      if (!$debug) {
	 `mv "$DIR_DOWNLOAD/$file" "$DIR_SERIES/$dir/$PREFIX_SEASON$season/$file"`;
      } 
   }
}

#
# Close the file if opened
#
if ($cron == 1) { 
   cron_log("####################################");
   close(FILE); 
}
